package xyz.xtt.pay.service;

import java.math.BigDecimal;
import java.util.Map;

import jakarta.validation.Valid;
import xyz.xtt.asset.client.response.AssetInfoResponse;
import xyz.xtt.asset.client.response.AssetTransactionResponse;
import xyz.xtt.pay.api.dto.GetPayStatusMapRequest;
import xyz.xtt.pay.api.dto.GetPayStatusRequest;
import xyz.xtt.pay.api.dto.PayAttach;
import xyz.xtt.pay.api.enums.PayOrderBusinessTypeEnum;
import xyz.xtt.pay.client.request.ConfirmPayOrderRequest;
import xyz.xtt.pay.client.request.MakeOrderRequest;
import xyz.xtt.pay.dto.OrderRefundMessage;
import xyz.xtt.pay.dto.PayDelayMessage;
import xyz.xtt.pay.dto.pay.GoodsInfo;
import xyz.xtt.pay.dto.pay.PayNotifyRequest;
import xyz.xtt.pay.entity.TblBusinessOrder;
import xyz.xtt.pay.entity.TblPayOrder;
import xyz.xtt.pay.vo.CreateOrderRequest;
import xyz.xtt.pay.vo.PayCallbackMessage;
import xyz.xtt.pay.vo.PayCenterArgsResponse;
import xyz.xtt.pay.vo.PayCenterArgsResponseV3;
import xyz.xtt.pay.vo.PayContinueRequest;
import xyz.xtt.pay.vo.paycenter.PayOrderCancelRequest;
import xyz.xtt.pay.vo.paycenter.PayOrderStatusChangeResponse;

/**
 * @author dale
 * @date 2025/1/13
 **/
public interface INftOrderService {

	/**
	 * 前端创建订单
	 * 
	 * @param uid
	 * @param makeOrder
	 * @return
	 */
	PayCenterArgsResponse createPayOrder(String uid, @Valid CreateOrderRequest makeOrder, GoodsInfo goodsInfo);

	TblPayOrder saveUnPayOrder(AssetTransactionResponse assetTransaction, PayOrderBusinessTypeEnum businessTypeEnum, String businessRefId, BigDecimal payMoney,
			PayAttach payAttach);

	TblPayOrder saveUnPayOrder(PayOrderBusinessTypeEnum businessTypeEnum, String businessRefId, BigDecimal payMoney, Long buyerUserId);

	TblPayOrder saveUnPayOrder(PayOrderBusinessTypeEnum businessTypeEnum, String businessRefId, BigDecimal payMoney, Long buyerUserId, String businessAttach);

	/**
	 * 支付延迟消息处理
	 * 
	 * @param payDelayMessage 消息
	 */
	void payDelayCancel(PayDelayMessage payDelayMessage);

	/**
	 * 支付回调消费
	 * 
	 * @param callbackMessage 回调消息
	 */
	void payCallbackConsume(PayCallbackMessage callbackMessage, boolean innerInvoke);
	
	boolean skipRealPay();

	void saveUpChainFlag(TblPayOrder payOrder);

	void savePaySuccessRecord(TblPayOrder payOrder, PayCallbackMessage callbackMessage);

	/**
	 * 订单退款
	 *
	 * @param orderRefundMessage 退款申请消息
	 */
	void refundOrder(OrderRefundMessage orderRefundMessage);

	/**
	 * 支付单状态确认
	 * 
	 * @param confirmRequest
	 * @return
	 */
	Boolean confirmPayStatus(@Valid ConfirmPayOrderRequest confirmRequest);

	/**
	 * 是否可以操作支付
	 * 
	 * @return
	 */
	Boolean canOperatePay(String uid);

	/**
	 * 手动取消订单
	 * 
	 * @param uid        用户id
	 * @param payOrderId 支付单id
	 * @return 支付单状态变更响应
	 */
	PayOrderStatusChangeResponse cancelOrder(String uid, PayOrderCancelRequest payOrderId);

	/**
	 * 未支付继续支付
	 * 
	 * @param uid
	 * @param continueRequest
	 * @return
	 */
	PayCenterArgsResponse continuePay(String uid, PayContinueRequest continueRequest);

	/**
	 * 获取支付订单状态
	 * 
	 * @param request
	 * @return
	 */
	Integer getPayStatus(GetPayStatusRequest request);

	/**
	 * 批量获取支付订单状态
	 * 
	 * @param request
	 * @return
	 */
	Map<Long, Integer> getPayStatusMap(@Valid GetPayStatusMapRequest request);

	boolean alreadyApplyRefund(Long payOrderId);

	void startRefundOrder(TblPayOrder payOrder, PayCallbackMessage callbackMessage);

	/**
	 * 保存未支付订单
	 * 
	 * @param assetTransaction
	 * @param businessTypeEnum
	 * @param businessRefId
	 * @param payMoney
	 * @param asset
	 * @return
	 */
	TblPayOrder saveUnPayOrder(AssetTransactionResponse assetTransaction, PayOrderBusinessTypeEnum businessTypeEnum, String businessRefId, BigDecimal payMoney,
			AssetInfoResponse asset);

	/**
	 * 发送支付延时消息
	 * 
	 * @param payOrder
	 */
	void sendPayDelayMessage(TblPayOrder payOrder);

	/**
	 * 保存自动支付成功订单
	 * 
	 * @param payOrderBusinessTypeEnum
	 * @param businessRefId
	 * @param price
	 * @param appUserId
	 * @return
	 */
	long saveAutoSuccessPayOrder(PayOrderBusinessTypeEnum payOrderBusinessTypeEnum, String businessRefId, BigDecimal price, Long appUserId);

	/**
	 * 修复部分历史订单支付中心数据
	 * 
	 * @return
	 */
	Boolean fixPayCenterData();

	/**
	 * 测试用户重置价格
	 * 
	 * @param appUserId
	 * @param realUnitPrice
	 * @return
	 */
	BigDecimal testUserResetPrice(Long appUserId, BigDecimal realUnitPrice);

	PayCenterArgsResponseV3 createPayOrderV3(String uid, @Valid CreateOrderRequest makeOrder);

	/**
	 * 支付回调
	 * 
	 * @param request
	 */
	void payNotify(PayNotifyRequest request);

	PayCenterArgsResponseV3 continuePayV3(String uid, @Valid PayContinueRequest continueRequest, TblPayOrder payOrder);

	/**
	 * 生成订单
	 * 
	 * @param uid
	 * @param makeOrderRequest
	 * @return
	 */
	TblBusinessOrder makeBusinessOrder(String uid, @Valid MakeOrderRequest makeOrderRequest);
	
	/**
	 * 生成支付参数
	 *
	 * @param uid
	 * @param businessOrder
	 * @param backUrl
	 * @return
	 */
	PayCenterArgsResponseV3 generatePayParam(String uid, TblBusinessOrder businessOrder, String backUrl);

	/**
	 * 生产支付参数
	 *
	 * @param uid
	 * @param payOrder
	 * @param backUrl
	 * @return
	 */
	PayCenterArgsResponseV3 generatePayParam(String uid, TblPayOrder payOrder, String backUrl);

	/**
	 * 订单支付成功处理
	 *
	 * @param payCallbackMessage
	 */
	void paySuccess(PayCallbackMessage payCallbackMessage, boolean needReCheck);
	
	/**
	 * 取消业务订单
	 * @param businessOrderId 业务订单id
	 * @param reasonRemark 取消原因备注
	 */
	void cancelBusinessOrder(Long businessOrderId, String reasonRemark);

	/**
	 * 支付结果回调处理
	 * @param request
	 */
	void payNotifyV2(PayNotifyRequest request);

	Boolean confirmPayStatusV2(ConfirmPayOrderRequest confirmRequest);

}
